package com.cn.xact.ty_treasured_book.activity;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.TYApplicationState;
import com.cn.xact.ty_treasured_book.R.layout;
import com.cn.xact.ty_treasured_book.R.menu;
import com.cn.xact.ty_treasured_book.config.TYConfigBase;
import com.cn.xact.ty_treasured_book.net.NetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkImpl;
import com.cn.xact.ty_treasured_book.sqlite.TYDBManager;
import com.cn.xact.ty_treasured_book.util.UIHelper;
import com.cn.xact.ty_treasured_book.TYUserState;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class HomeActivity extends Activity {

    private TextView mPhoneName;
    private NetworkImpl mHomeNetwork;
    private String[] mSupportedPhones;
    private Button mLoginBt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_home_list);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.main_home);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);
        title1.setText(R.string.main_page);

        mPhoneName = (TextView)findViewById(R.id.main_home_sel_phone);
        mLoginBt = (Button)findViewById(R.id.home_login_bt);

        initPage(TYUserState.isUserLoggedIn());

        mHomeNetwork = new NetworkImpl(this, new MyNetworkCallBack());

        //mSupportedPhones = TYApplicationState.getSupportedPhones();

        //if (mSupportedPhones == null) {
            mHomeNetwork.getSupportPhonesAsync(123);
        //}

        mLoginBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (TYUserState.isUserLoggedIn()) {
                    initPage(false);
                    TYUserState.userLogOut();
                } else {
                    Intent tmp = new Intent(HomeActivity.this, LoginActivity.class);
                    startActivity(tmp);
                }
            }
        });
    }

    private void updatePhoneName() {
        String selPhoneAlert = this.getResources().getString(R.string.home_user_choose_phone);
        SharedPreferences sp = getSharedPreferences(TYConfigBase.TY_APPLICATION_STATUS_SP, Context.MODE_PRIVATE);
        String name2Show = sp.getString(TYConfigBase.TY_APPLICATION_STATUS_CURRENT_PHONE, null);

        if (name2Show != null) {
            mPhoneName.setText(name2Show);
        } else {
            mPhoneName.setText(selPhoneAlert);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        updatePhoneName();
    }

    private void initPage(boolean isUserLogged) {

        ImageView searchBt = (ImageView)findViewById(R.id.main_home_sel_phone_indicator);

        TextView nickName = (TextView)findViewById(R.id.home_user_nick);

        Button exchangeList    = (Button)findViewById(R.id.home_exchange);
        Button exchangehistory = (Button)findViewById(R.id.home_credit_history);
        Button billBoard       = (Button)findViewById(R.id.home_billboard);
        Button creditRules     = (Button)findViewById(R.id.home_credit_rules);
        Button userSettings    = (Button)findViewById(R.id.home_personal_setting);
        Button pswSettings     = (Button)findViewById(R.id.home_change_psw);

        searchBt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent tmp = new Intent(HomeActivity.this, HomePhoneSelActivity.class);
                tmp.putExtra("phones", mSupportedPhones);
                tmp.putExtra("bootmain", false);
                startActivity(tmp);
            }

        });

        exchangeList.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent tmp = new Intent(HomeActivity.this, HomeCreditExchangeListActivity.class);
                startActivity(tmp);
            }

        });

        exchangehistory.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent tmp = new Intent(HomeActivity.this, HomeCreditHistoryActivity.class);
                startActivity(tmp);
            }

        });

        billBoard.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent tmp = new Intent(HomeActivity.this, HomeBillBoardActivity.class);
                startActivity(tmp);
            }

        });

        creditRules.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent tmp = new Intent(HomeActivity.this, HomeCreditRuleActivity.class);
                startActivity(tmp);
            }

        });

        if (!isUserLogged) {
            Button mySettingBT = (Button)findViewById(R.id.home_personal_setting);
            mySettingBT.setVisibility(View.INVISIBLE);

            Button ChangePSWBT = (Button)findViewById(R.id.home_change_psw);
            ChangePSWBT.setVisibility(View.INVISIBLE);

            nickName.setText(R.string.tourist);
            mLoginBt.setText(getResources().getString(R.string.login));
        } else {
            mLoginBt.setText(getResources().getString(R.string.logout));

            nickName.setText(TYUserState.getUserNick());

            userSettings.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent tmp = new Intent(HomeActivity.this, HomeSettingsActivity.class);
                    startActivity(tmp);
                }

            });

            pswSettings.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent tmp = new Intent(HomeActivity.this, HomePSWSettingsActivity.class);
                    startActivity(tmp);
                }

            });

            TextView loginCounter = (TextView)findViewById(R.id.home_login_counter);
            loginCounter.setText(getResources().getString(R.string.login) + " : " + TYUserState.getUserLoginCount());
            TextView credit = (TextView)findViewById(R.id.home_credit_counter);
            credit.setText(getResources().getString(R.string.home_user_credit) + " : " + TYUserState.getUserCredit());
            TextView rank = (TextView)findViewById(R.id.home_range_counter);
            rank.setText(getResources().getString(R.string.home_user_range) + " : " + TYUserState.getUserRank());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    class MyNetworkCallBack extends NetworkCallBack {
        public void onNetworkCallBack(int id, boolean success, Object attach1, Object attach2) {
            if (!success) {
                UIHelper.showConfirmDialog(HomeActivity.this, null, getResources().getString(R.string.server_busy));
                return;
            }
            switch(id) {
            case NetworkImpl.NET_WORK_ID_GET_SUPPORT_PHONES:
                mSupportedPhones = (String[])attach1;
                TYApplicationState.updateSupportedPhones(mSupportedPhones);
                break;
            }
        }
    }

}
